package cn.xutongxue.user.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.xutongxue.common.entity.ResponseResult;
import cn.xutongxue.user.enums.UserRole;
import cn.xutongxue.user.module.dto.RegisterDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.xutongxue.user.entity.User;
import cn.xutongxue.user.service.UserService;
import cn.xutongxue.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

/**
* @author PC
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-06-01 13:24:35
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public ResponseResult<Boolean> register(RegisterDto registerDto) {

        // 判断用户是否存在
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, registerDto.getUsername());
        User checkUser = this.getOne(wrapper);
        if (checkUser != null) {
            return ResponseResult.error("用户已存在");
        }
        // 加密密码
        String encryptPassword = SaSecureUtil.md5(registerDto.getPassword());
        User user = new User();
        user.setUsername(registerDto.getUsername());
        user.setPassword(encryptPassword);
        // 设置默认角色
        user.setUserRole(UserRole.USER.getRoleKey());
        // 保存用户
        this.save(user);

        return ResponseResult.success(true);
    }
}




